const {User} = require('../../model/user')

module.exports = async (req,res,next) => {
    const id = req.query.id
    //获取请求参数
    const {username,email,role,state,password} = req.body

    const user = await User.findOne({_id: id})
    //密码比对
    if(password == user.password) {
        // res.send('密码输入正确')
        //更新用户数据
        await User.updateOne({_id: id}, {
            username: username,
            email: email,
            role: role,
            state: state
        });
        //重定向
        res.redirect('/admin/user');
    }else {
        // res.send('密码输入错误')
        let obj = {path: '/admin/user-edit',message: '密码输入错误，禁止修改用户',id: id}
        next(JSON.stringify(obj));
    }
}